<?php

/**
 * Description of AjaxianResponse
 *
 * @author bonfiglio
 */
class AjaxianResponse {

    public $data;
    public $fields;
    public $error;
    public $success;

    public function __construct($data = array(), $successMessage = false, $errorMessage = false, $fields = array()) {
        $this->set($data, $successMessage, $errorMessage, $fields);
        $this->logout = false;
    }
    
    public function set($data = array(), $successMessage = false, $errorMessage = false, $fields = array()){
        $this->setData($data);
        $this->setError($errorMessage);
        $this->setSuccess($successMessage);
        $this->setFields($fields);
    }

    public function setSuccess($message) {
        if (!$this->hasError()) {
            $this->success = filter_var($message, FILTER_SANITIZE_STRING);
        }
        return $this;
    }
    
    public function setError($errorMessage) {
        $this->error = $errorMessage;
        return $this;
    }
    
    public function setData($data) {
        $this->data = $data;
        return $this;
    }
    
    public function setFields($fields) {
        $this->fields = $fields;
        return $this;
    }

    public function setLogout() {
        $this->logout = true;
        return $this;
    }

    public function hasError() {
        return (boolean) $this->error;
    }

    public function getError() {
        return $this->error;
    }

    public function getSuccess() {
        return $this->success;
    }

    public function getFields() {
        return $this->fields;
    }

    public function getData() {
        return $this->data;
    }

    public function jsonData($alt = false) {
        Page::sendJson($this, $alt);
    }

}

?>
